﻿Imports Infragistics.Win.UltraWinGrid

Public Class KeToanTienMat1
    'state : 0 mặc định - trạng thái 1- tạo mới 2 sửa 3 - xóa
    Private state As Integer
    'load form
    Dim isLoad As Boolean = True
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = (Keys.Control Or Keys.N) Then
            Return getTaoMoi()
        End If
        If keyData = (Keys.Control Or Keys.S) Then
            Return getLuu()
        End If
        If keyData = (Keys.Control Or Keys.E) Then
            Return getSua()
        End If
        If keyData = (Keys.Control Or Keys.D) Then
            Return Me.KetoantienmatIUltraGrid_ThongTin.getXoa()
        End If
        If keyData = (Keys.Control Or Keys.P) Then
            'Return Me.getIn()
        End If
        If keyData = (Keys.Control Or Keys.R) Then
            Return Me.KetoantienmatIUltraGrid_ThongTin.getRefresh()
        End If
        If (keyData = (Keys.Control Or Keys.Q)) Then
            Return getThoat()
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
    Private Sub loadDuLieu()
        'If isLoad Then
        'Me.Cursor = Cursors.WaitCursor
        'Dim load As UltraActivityIndicator = New UltraActivityIndicator
        'load.Show()
        Me.KetoantienmatTableAdapter.Fill(Me.ErpharmaDataSet.ketoantienmat)
        Me.Ketoantienmat_thongtinthueTableAdapter.Fill(Me.ErpharmaDataSet.ketoantienmat_thongtinthue)
        Me.Ketoantienmat_thongtinchinhTableAdapter.Fill(Me.ErpharmaDataSet.ketoantienmat_thongtinchinh)
        Me.DmnhanvienTableAdapter.Fill(Me.ErpharmaDataSet.dmnhanvien)
        Me.DmphongbanTableAdapter.Fill(Me.ErpharmaDataSet.dmphongban)
        Me.DmloaitienTableAdapter.Fill(Me.ErpharmaDataSet.dmloaitien)
        Me.DmkhachhangTableAdapter.Fill(Me.ErpharmaDataSet.dmkhachhang)
        Me.DmtinhtrangphieuTableAdapter.Fill(Me.ErpharmaDataSet.dmtinhtrangphieu)
        Me.Dmtaikhoancap2TableAdapter.Fill(Me.ErpharmaDataSet.dmtaikhoancap2)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.ErpharmaDataSet.dmkhoanmucphi)
        Me.DmnghiepvuTableAdapter.Fill(Me.ErpharmaDataSet.dmnghiepvu)

        'Me.DmnghiepvuTableAdapter.Fill(Me.ErpharmaDataSet.dmnghiepvu)
        'Me.DmnhanvienTableAdapter.Fill(Me.ErpharmaDataSet.dmnhanvien)
        'Me.DmphongbanTableAdapter.Fill(Me.ErpharmaDataSet.dmphongban)
        'Me.DmloaitienTableAdapter.Fill(Me.ErpharmaDataSet.dmloaitien)
        'Me.DmkhachhangTableAdapter.Fill(Me.ErpharmaDataSet.dmkhachhang)
        'Me.DmtinhtrangphieuTableAdapter.Fill(Me.ErpharmaDataSet.dmtinhtrangphieu)
        'Me.Dmtaikhoancap2TableAdapter.Fill(Me.ErpharmaDataSet.dmtaikhoancap2)
        'set giao diện
        'Me.isLoad = False
        'End If
    End Sub
    Private Sub setDoiTuong()
        Me.KetoantienmatIUltraGrid_ThongTin.setGiaoDien_Doi_ThongTin()
        Me.KetoantienmatIUltraGrid_DanhSach.setGiaoDienTuyBien()
        Me.DmnghiepvuIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.DmnhanvienIUltraDropDown.setGiaoDien()
        Me.DmphongbanIUltraDropDown.setGiaoDien()
        Me.DmloaitienIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.DmkhachhangIUltraDropDown.setGiaoDien()
        Me.DmtinhtrangphieuIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setGiaoDienTuyBien()
        Me.Ketoantienmat_thongtinthueIUltraGrid.setGiaoDienTuyBien()
        Me.DmkhachhangIUltraGrid.setGiaoDienTuyBien()
        Me.DmkhachhangIUltraGrid1.setGiaoDienTuyBien()
        Me.Dmtaikhoancap2IUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.Dmtaikhoancap2IUltraDropDown1.setGiaoDien_An_ColumnHeader()
        Me.DmkhoanmucphiIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.DmLoaiDoanhThuThue.setGiaoDien()
        'set tìm kiếm
        'Me.DmkhachhangIUltraGrid.setTimKiemTrenCotUltraGrid()
        'Me.DmkhachhangIUltraGrid1.setTimKiemTrenCotUltraGrid()
        'set doi tuong 
        'Me.DmkhachhangIUltraGrid.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmkhachhang, Me.DmkhachhangBindingSource, Me.DmkhachhangTableAdapter, Me.ErpharmaDataSet)
        'Me.DmkhachhangIUltraGrid1.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmkhachhang, Me.DmkhachhangBindingSource, Me.DmkhachhangTableAdapter, Me.ErpharmaDataSet)
        'menustrip
        Me.DmkhachhangIUltraGrid.setChoPhepMenuStrip()
        'Me.DmkhachhangIUltraGrid1.setChoPhepMenuStrip()
        'ultracomboeditor
        'Me.DmkhachhangIUltraComBoEditor.setThemUltraGrid(Me.DmkhachhangIUltraGrid)
        'Me.DmkhachhangIUltraComBoEditor1.setThemUltraGrid(Me.DmkhachhangIUltraGrid1)
        'danh sách
        Me.KetoantienmatIUltraGrid_DanhSach.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.ketoantienmat, Me.KetoantienmatBindingSource, Me.KetoantienmatTableAdapter, Me.ErpharmaDataSet)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmnghiepvuIUltraDropDown, Me.ErpharmaDataSet.dmnghiepvu)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmnhanvienIUltraDropDown, Me.ErpharmaDataSet.dmnhanvien)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmphongbanIUltraDropDown, Me.ErpharmaDataSet.dmphongban)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmloaitienIUltraDropDown, Me.ErpharmaDataSet.dmloaitien)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmkhachhangIUltraDropDown, Me.ErpharmaDataSet.dmkhachhang)
        Me.KetoantienmatIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmtinhtrangphieuIUltraDropDown, Me.ErpharmaDataSet.dmtinhtrangphieu)
        Me.KetoantienmatIUltraGrid_DanhSach.setTimKiemTrenCotUltraGrid()
        KetoantienmatIUltraGrid_DanhSach.setSummaries("nguyente")
        KetoantienmatIUltraGrid_DanhSach.setSummaries("tienvnd")
        'thông tin
        Me.KetoantienmatIUltraGrid_ThongTin.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.ketoantienmat, Me.KetoantienmatBindingSource, Me.KetoantienmatTableAdapter, Me.ErpharmaDataSet)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmnghiepvuIUltraDropDown, Me.ErpharmaDataSet.dmnghiepvu)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmnhanvienIUltraDropDown, Me.ErpharmaDataSet.dmnhanvien)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmphongbanIUltraDropDown, Me.ErpharmaDataSet.dmphongban)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmloaitienIUltraDropDown, Me.ErpharmaDataSet.dmloaitien)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmkhachhangIUltraDropDown, Me.ErpharmaDataSet.dmkhachhang)
        Me.KetoantienmatIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmtinhtrangphieuIUltraDropDown, Me.ErpharmaDataSet.dmtinhtrangphieu)
        ' Me.KetoantienmatIUltraGrid_ThongTin.setUltraComboEditor(Me.DmkhachhangIUltraComBoEditor)
        'gán mặc định
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("loaitien", "VND")
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("tygia", 1)
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("nguyente", 0)
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("tienvnd", 0)
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("sochungtu", "")
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("ngaylap", Today)
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("ngaylapthamchieu", Today)
        'bắt lỗi
        'set cot bat loi
        Me.KetoantienmatIUltraGrid_ThongTin.setCotBatLoi("nghiepvu")
        Me.KetoantienmatIUltraGrid_ThongTin.setCotBatLoi("donvi")
        Me.KetoantienmatIUltraGrid_ThongTin.setCotBatLoi("tinhtrang")
        'thông tin chính
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.ketoantienmat_thongtinchinh, Me.Ketoantienmat_thongtinchinhBindingSource, Me.Ketoantienmat_thongtinchinhTableAdapter, Me.ErpharmaDataSet)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.Dmtaikhoancap2IUltraDropDown, Me.ErpharmaDataSet.dmtaikhoancap2)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.Dmtaikhoancap2IUltraDropDown1, Me.ErpharmaDataSet.dmtaikhoancap2)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.DmkhachhangIUltraDropDown, Me.ErpharmaDataSet.dmkhachhang)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.DmphongbanIUltraDropDown, Me.ErpharmaDataSet.dmphongban)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.DmLoaiDoanhThuThue, Me.ErpharmaDataSet.dmloaidoanhthuthue)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setUltraComboEditor(Me.DmkhachhangIUltraComBoEditor1)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setThemIUltraDropDown(Me.DmkhoanmucphiIUltraDropDown, Me.ErpharmaDataSet.dmkhoanmucphi)
        Ketoantienmat_thongtinchinhIUltraGrid.setChoPhepMenuStrip()

        'mặc định
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("nguyente", 0)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("tienvnd", 0)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("ngay", Today)
        'summaries thông tin chính
        'thêm tổng summaries
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setSummaries("nguyente")
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setSummaries("tienvnd")
        'thông tin khai thuế
        Me.Ketoantienmat_thongtinthueIUltraGrid.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.ketoantienmat_thongtinthue, Me.Ketoantienmat_thongtinthueBindingSource, Me.Ketoantienmat_thongtinthueTableAdapter, Me.ErpharmaDataSet)
        Me.Ketoantienmat_thongtinthueIUltraGrid.setChoPhepMenuStrip()
        Me.Ketoantienmat_thongtinthueIUltraGrid.setThemIUltraDropDown(Me.DmkhachhangIUltraDropDown, Me.ErpharmaDataSet.dmkhachhang)
        'Me.KetoantienmatBindingSource.MoveLast()
        'If KetoantienmatIUltraGrid_ThongTin.Rows.Count > 0 Then
        '    KetoantienmatIUltraGrid_ThongTin.ActiveRowScrollRegion.ScrollRowIntoView(Me.KetoantienmatIUltraGrid_ThongTin.Rows(Me.KetoantienmatIUltraGrid_ThongTin.Rows.Count - 1))
        'End If
        Me.KetoantienmatIUltraGrid_ThongTin.setMoveLast()
        'Me.DmkhachhangBindingSource.Filter = "manhomkh = '" &  & "'" 
        'tìm kiếm
        Me.Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.setGiaoDienTuyBien()
        Me.Ketoantienmat_thongtinthueIUltraGrid_TimKiem.setGiaoDienTuyBien()
        Me.Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.setTimKiemTrenCotUltraGrid()
        Me.Ketoantienmat_thongtinthueIUltraGrid_TimKiem.setTimKiemTrenCotUltraGrid()
        Me.Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.setGroupBy("ma")
        Me.Ketoantienmat_thongtinthueIUltraGrid_TimKiem.setGroupBy("ma")
        Ketoantienmat_thongtinthueIUltraGrid_TimKiem.setReadonly(True)
        Me.Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.setReadonly(True)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setTimKiemTrenCotUltraGrid()
        'thêm tổng summaries
        Me.Ketoantienmat_thongtinthueIUltraGrid.setSummaries("doanhthu")
        Me.Ketoantienmat_thongtinthueIUltraGrid.setSummaries("thue")
        Me.Ketoantienmat_thongtinthueIUltraGrid.setSummaries("tongcong")
        Me.Ketoantienmat_thongtinthueIUltraGrid.setTimKiemTrenCotUltraGrid()

        Me.KetoantienmatIUltraGrid_ThongTin.setTimKiemTheoShift()
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setTimKiemTheoShift()
        Me.setReadOnly(True)
        'Me.Cursor = Cursors.Default
        'Me.ultraProgressBar1.Value = 100
        ' Me.ultraProgressBar1.Hide()
        'load.Close()
    End Sub
    Private Sub KeToanTienMat1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ErpharmaDataSet.dmloaidoanhthuthue' table. You can move, or remove it, as needed.
        Me.DmloaidoanhthuthueTableAdapter.Fill(Me.ErpharmaDataSet.dmloaidoanhthuthue)
        Me.Timer1.Start()
        '' Me.bgwData.RunWorkerAsync()
        'Me.loadDanhMuc()
        'Me.loadDuLieu()
    End Sub
    'để cho lưới readonly
    Private Sub setReadOnly(ByVal isTrue As Boolean)
        Me.KetoantienmatIUltraGrid_ThongTin.setReadonly(isTrue)
        Me.Ketoantienmat_thongtinchinhIUltraGrid.setReadonly(isTrue)
        Me.Ketoantienmat_thongtinthueIUltraGrid.setReadonly(isTrue)
        Me.KetoantienmatIUltraGrid_DanhSach.setReadonly(isTrue)
    End Sub
    Private Function getTaoMoi()
        Me.KetoantienmatIUltraGrid_ThongTin.setMacDinh("ma", getMa)
        If (Me.KetoantienmatIUltraGrid_ThongTin.getTaoMoi()) Then
            Me.setReadOnly(False)
            Me.state = 1
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub btnTaoMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTaoMoi.Click
        getTaoMoi()
    End Sub
    Private Function getLuu()
        Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("nguyente").Value = Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows.SummaryValues("nguyente").Value
        Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("tienvnd").Value = Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows.SummaryValues("tienvnd").Value
        If Me.KetoantienmatIUltraGrid_ThongTin.getLuu() Or Me.Ketoantienmat_thongtinchinhIUltraGrid.getLuuNgam() Or Me.Ketoantienmat_thongtinthueIUltraGrid.getLuuNgam() Then
            Me.setReadOnly(True)
            Me.state = 0
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        getLuu()
    End Sub
    Private Function getSua()
        If KetoantienmatIUltraGrid_ThongTin.getSua() And Me.Ketoantienmat_thongtinchinhIUltraGrid.getSua And Me.Ketoantienmat_thongtinthueIUltraGrid.getSua Then
            Me.setReadOnly(False)
            Me.state = 2
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        getSua()
    End Sub
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        KetoantienmatIUltraGrid_ThongTin.getXoa()
    End Sub

    Private Sub btnLamTuoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamTuoi.Click
        Me.KetoantienmatIUltraGrid_ThongTin.getRefresh()
    End Sub

    Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
        'Me.Ketoantienmat_thongtinthueIUltraGrid.DisplayLayout.Bands(0).AddNew()
        'Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("ma").Value = Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("ma").Value
        ''Me.Ketoantienmat_thongtinthueIUltraGrid.Rows().Move(row, 0)
        ''Ketoantienmat_thongtinthueIUltraGrid.setGroupBy()
    End Sub
    Private Function getThoat()
        If Me.KetoantienmatIUltraGrid_ThongTin.getCoThayDoiDuLieuKhong Or Me.Ketoantienmat_thongtinchinhIUltraGrid.getCoThayDoiDuLieuKhong Or Me.Ketoantienmat_thongtinthueIUltraGrid.getCoThayDoiDuLieuKhong Then
            If KetoantienmatIUltraGrid_ThongTin.getLuu Then
                Ketoantienmat_thongtinchinhIUltraGrid.getLuuNgam()
                Ketoantienmat_thongtinthueIUltraGrid.getLuuNgam()
            End If
            Return True
        Else
            Me.Dispose()
            Me.Close()
            Return True
        End If
    End Function
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        getThoat()
    End Sub
    'mã tự động của phiếu
    Private Function getMa() As String
        'Try
        '    Dim ma As String = Me.HttientoTableAdapter.getMaKeToanTienMat(New Date(2012, 4, 1), New Date(2012, 4, 30))
        '    Return ma
        'Catch ex As Exception
        '    Return Today.Year & Today.Month & Today.Day
        'End Try
        Return ""
    End Function

    Private Sub KetoantienmatIUltraGrid_ThongTin_AfterCellListCloseUp(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles KetoantienmatIUltraGrid_ThongTin.AfterCellListCloseUp
        Try
            If KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("nghiepvu").IsActiveCell Then
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("taikhoanno", Me.DmnghiepvuIUltraDropDown.ActiveRow.Cells("taikhoanno").Value)
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("taikhoanco", Me.DmnghiepvuIUltraDropDown.ActiveRow.Cells("taikhoanco").Value)
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("taikhoanvat", Me.DmnghiepvuIUltraDropDown.ActiveRow.Cells("taikhoanvat").Value)
            End If
            If KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("donvi").IsActiveCell Then
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("donvi", Me.DmkhachhangIUltraDropDown.ActiveRow.Cells("ma").Value)
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("diachi", Me.DmkhachhangIUltraDropDown.ActiveRow.Cells("diachi").Value)
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("masothue", Me.DmkhachhangIUltraDropDown.ActiveRow.Cells("masothue").Value)
            End If
            If KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("bophan").IsActiveCell Then
                Me.Ketoantienmat_thongtinchinhIUltraGrid.setMacDinh("phongban", Me.DmphongbanIUltraDropDown.ActiveRow.Cells("ma").Value)
            End If
        Catch ex As Exception
            ' MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub KetoantienmatIUltraGrid_ThongTin_BeforeCellDeactivate(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles KetoantienmatIUltraGrid_ThongTin.BeforeCellDeactivate
        If Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("nghiepvu").IsActiveCell Then
            Me.KetoantienmatIUltraGrid_ThongTin.getLuuNgam()
        End If
        If Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("tinhtrang").IsActiveCell And (state = 2 Or state = 1) Then
            Me.KetoantienmatIUltraGrid_ThongTin.getLuuNgam()
            Me.KetoantienmatIUltraGrid_ThongTin.setFocusCellUltraGrid(Me.Ketoantienmat_thongtinchinhIUltraGrid)
        End If
    End Sub
    Private Sub tinhDong_ThongTinChinh()
        If Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("nguyente").IsActiveCell Then
            Try
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("tienvnd").Value = CDec(KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("tygia").Value) * CDec(Me.Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("nguyente").Value)
            Catch ex As Exception
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("tienvnd").Value = 0
            End Try
        End If
    End Sub

    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_AfterCellListCloseUp(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.AfterCellListCloseUp
        If Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("donvi").IsActiveCell Then
            Try
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("diachi").Value = Me.DmkhachhangIUltraDropDown.ActiveRow.Cells("diachi").Value
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("masothue").Value = Me.DmkhachhangIUltraDropDown.ActiveRow.Cells("masothue").Value
            Catch ex As Exception
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("diachi").Value = ""
                Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("masothue").Value = ""
            End Try
           
        End If
    End Sub

    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_AfterCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.AfterCellUpdate
        tinhDong_ThongTinChinh()
    End Sub
    Private Function getBatLoiTKNo() As Boolean
        'If Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Index <> -1 Then
        '    If (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "642" Or Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "331" Or Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "641" Or Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "221") Then
        '        Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("loai").Value = "1"
        '        ' Or (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("loai").Value = "2" And (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "133")) Then
        '        Return True
        '    ElseIf (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "133") Then
        '        Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("loai").Value = "2"
        '        ' Or (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("loai").Value = "2" And (Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").Value.ToString.Substring(0, 3) = "133")) Then
        '        Return True
        '    Else
        '        MessageBox.Show("'Tài khoản nợ ' bạn đã chọn sai !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
        '        Return False
        '    End If

        'End If
        Return True
    End Function
    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_BeforeCellDeactivate(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.BeforeCellDeactivate
        If Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("taikhoanno").IsActiveCell Then
            If getBatLoiTKNo() = False Then
                e.Cancel = True
            End If
        End If
    End Sub
    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_DoubleClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickCellEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.DoubleClickCell
        If Me.Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Index = 0 And (state = 2 Or state = 1) Then
            Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("donvi").Value = Me.Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("donvi").Value
        End If
    End Sub

    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.MouseEnter
        ' Me.loadDuLieu()
    End Sub
    Private Sub tinhDong_ThongTinThue() 'tính doanh thu nếu gõ %thue
        If Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("phantramthue").IsActiveCell Then
            Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").Value = CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("tongcong").Value) * CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("phantramthue").Value) / 100
            Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("doanhthu").Value = CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("tongcong").Value) - CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").Value)
        End If
        If Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").IsActiveCell Then
            Dim thue As Decimal = CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").Value)
            Dim tongcong As Decimal = CDec(Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("tongcong").Value)
            Dim phantramthue As Integer = Math.Round(thue / tongcong * 100, 0)
            Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("doanhthu").Value = tongcong - thue
            Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("phantramthue").Value = phantramthue
        End If
    End Sub
    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_SummaryValueChanged(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.SummaryValueChangedEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid.SummaryValueChanged
        'Try
        '    Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("nguyente").Value = Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows.SummaryValues("nguyente").Value
        '    Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("tienvnd").Value = Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows.SummaryValues("tienvnd").Value
        'Catch ex As Exception

        'End Try
    End Sub

    Private Sub ultraTabChiTiet_SelectedTabChanged(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinTabControl.SelectedTabChangedEventArgs) Handles ultraTabChiTiet.SelectedTabChanged
        Dim groupByRow As UltraGridGroupByRow
        Dim groupByRowtHUE As UltraGridGroupByRow
        Dim sumDoanhThu As Double
        Dim sumThue As Double
        If Me.ultraTabChiTiet.SelectedTab Is ultraTabChiTiet.Tabs("thongtinthue") Then
            Me.Ketoantienmat_thongtinchinhIUltraGrid.setGroupBy("loai")
            Me.Ketoantienmat_thongtinchinhIUltraGrid.setGroupBy("sohoadon")
            Me.Ketoantienmat_thongtinchinhIUltraGrid.setGroupBy("ngay")
            If Ketoantienmat_thongtinchinhIUltraGrid.getCoThayDoiDuLieuKhong Then
                If MessageBox.Show("Bạn có muốn lấy dữ liệu sang thuế tự động hay không ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then

                    For index = 0 To Ketoantienmat_thongtinchinhIUltraGrid.Rows.Count - 1
                        Dim gbr As UltraGridGroupByRow = CType(Ketoantienmat_thongtinchinhIUltraGrid.Rows(index), UltraGridGroupByRow)
                        'If gbr.HasChild Then
                        'End If
                        For index1 = 0 To gbr.ChildBands(0).Rows().Count - 1
                            'MessageBox.Show(gbr.Rows(index1).ChildBands(0).Rows.SummaryValues("tienvnd").Value)
                            Try
                                MessageBox.Show(gbr.ChildBands(0).Rows(index1).ChildBands(0).Rows.SummaryValues("tienvnd").Value)
                                MessageBox.Show(gbr.Rows(index).ChildBands.FirstRow.ChildBands.FirstRow.Cells("donvi").Value)
                            Catch ex As Exception

                            End Try

                        Next
                    Next



                    'MessageBox.Show(gbr.Rows().Count.ToString)

                    'Try
                    '    MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows(0).ChildBands(0).Rows.SummaryValues("tienvnd").Value)
                    'Catch ex As Exception
                    '    MessageBox.Show("0 " & " 0")
                    'End Try

                    'Try
                    '    MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows(0).ChildBands.FirstRow)
                    'Catch ex As Exception
                    '    MessageBox.Show("0 " & " 3")
                    'End Try
                    'For index = 0 To Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.ParentCollection.Count - 1
                    'For index = 0 To Ketoantienmat_thongtinchinhIUltraGrid.Rows.Count - 1
                    'MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows.Count) group lai 1 dong
                    'For index = 0 To Ketoantienmat_thongtinchinhIUltraGrid.Rows.GetAllNonGroupByRows.Length - 1
                    '    'groupByRow = Ketoantienmat_thongtinchinhIUltraGrid.Rows(index)

                    '    MessageBox.Show(groupByRow.Rows(index).ChildBands(0).Rows.SummaryValues("tienvnd").Value)
                    'Next
                    'MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows.GetAllNonGroupByRows.Length)
                    'If index Mod 2 = 0 Then 'số chẵn
                    '    MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows(index).ParentRow.ChildBands(1).Rows.SummaryValues("tienvnd").Value)
                    '    'groupByRow = Ketoantienmat_thongtinchinhIUltraGrid.Rows.IsGroupByRows
                    '    'If Ketoantienmat_thongtinchinhIUltraGrid.Rows.IsGroupByRows Then
                    '    '    MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows.Band.)
                    '    'End If
                    '    'If Ketoantienmat_thongtinchinhIUltraGrid.Rows.GetAllNonGroupByRows Then
                    '    '    MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows(index).Cells("donvi").Value)
                    '    'End If
                    '    'Dim rowsCount As Integer = 0
                    '    'Dim groupByRowsCount As Integer = 0

                    '    '' Get the first row in the UltraGrid.
                    '    'Dim firstRow As UltraGridRow = Me.Ketoantienmat_thongtinchinhIUltraGrid.GetRow(ChildRow.First)

                    '    '' Call the helper method which is a recursive implmentation for traversing rows.
                    '    'MessageBox.Show("Please wait. This operation may take a while depending on number of rows.")
                    '    'Me.TraverseAllRowsHelper(firstRow, rowsCount, groupByRowsCount)

                    '    '' Show a dialog showing the number of regular rows and number of group-by rows.
                    '    'MessageBox.Show("The UltraGrid has " & rowsCount & " number of regular rows, and " & groupByRowsCount & " number of group-by rows.")
                    '    'If Ketoantienmat_thongtinchinhIUltraGrid.Rows(index).IsGroupByRow = False Then
                    '    'MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.Rows(3).Cells("donvi").Value)
                    '    'End If
                    '    '    sumDoanhThu = groupByRow.Rows.SummaryValues("tienvnd").Value
                    '    '    'sumDoanhThu = groupByRow.Rows(1).ParentRow.ChildBands(1).Rows.SummaryValues("tienvnd").Value
                    '    '    'MessageBox.Show("DOANH THU " & Format(sumDoanhThu, "N0"))
                    'Else
                    '    Try
                    '        groupByRowtHUE = Ketoantienmat_thongtinchinhIUltraGrid.Rows(index).ChildBands.LastRow
                    '        sumThue = groupByRowtHUE.Rows.SummaryValues("tienvnd").Value
                    '        'MessageBox.Show("THUE " & Format(sumThue, "N0"))
                    '        Me.Ketoantienmat_thongtinthueIUltraGrid.DisplayLayout.Bands(0).AddNew()
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("ma").Value = Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("ma").Value
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("donvi").Value = groupByRow.Rows(0).ChildBands.FirstRow.Cells("donvi").Value
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("sohoadon").Value = groupByRow.Rows(0).ChildBands.FirstRow.Cells("sohoadon").Value
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("ngayhoadon").Value = groupByRow.Rows(0).ChildBands.FirstRow.Cells("ngay").Value
                    '        'dia chi
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("diachi").Value = groupByRow.Rows(0).ChildBands.FirstRow.Cells("diachi").Value
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("masothue").Value = groupByRow.Rows(0).ChildBands.FirstRow.Cells("masothue").Value
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("doanhthu").Value = sumDoanhThu
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("phantramthue").Value = sumThue / sumDoanhThu * 100
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").Value = sumThue
                    '        Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("tongcong").Value = sumDoanhThu + sumThue
                    '    Catch ex As Exception
                    '        MessageBox.Show(ex.Message)
                    '    End Try
                    'End If

                    'Next
                End If

                'Me.Ketoantienmat_thongtinchinhIUltraGrid.setGroupBy("sohoadon")
                'MessageBox.Show(Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows.SummaryValues.Item(0).Value)

                'For index = 0 To 1
                '    Me.Ketoantienmat_thongtinthueIUltraGrid.DisplayLayout.Bands(0).AddNew()
                '    Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("ma").Value = Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("ma").Value
                '    Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("doanhthu").Value = 0
                '    Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("phantramthue").Value = 0
                '    Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("thue").Value = 0
                '    Ketoantienmat_thongtinthueIUltraGrid.ActiveRow.Cells("tongcong").Value = 0
                'Next

                'Dim groupByRow As UltraGridGroupByRow = Ketoantienmat_thongtinchinhIUltraGrid.Rows(0)
                'Dim groupByRow1 As UltraGridGroupByRow = Ketoantienmat_thongtinchinhIUltraGrid.Rows(1)
                'MessageBox.Show(groupByRow.Rows.SummaryValues(2).Value)
                'MessageBox.Show(groupByRow1.Rows.SummaryValues(2).Value)
                'MessageBox.Show(Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.ParentCollection.Count)
            End If
        End If
        If Me.ultraTabChiTiet.SelectedTab Is ultraTabChiTiet.Tabs("thongtinchinh") Then
            'Me.Ketoantienmat_thongtinchinhIUltraGrid.DisplayLayout.ClearGroupByColumns()
            ''Ketoantienmat_thongtinchinhIUltraGrid.DisplayLayout.ResetGroupByBox()
            'Me.Ketoantienmat_thongtinchinhIUltraGrid.DisplayLayout.ViewStyleBand = 
            'Ketoantienmat_thongtinchinhIUltraGrid.setClearGroupBy()
        End If
    End Sub
    Private Sub TraverseAllRowsHelper(ByVal startRow As UltraGridRow, ByRef rowsCount As Integer, ByRef groupByRowsCount As Integer)

        Dim row As UltraGridRow = startRow
        Dim groupbyrow As UltraGridGroupByRow
        While Not row Is Nothing
            If TypeOf row Is UltraGridGroupByRow Then
                ' Write code here to process the group by row.
                groupByRowsCount += 1
                MessageBox.Show(groupByRowsCount.ToString)
                groupbyrow = Me.Ketoantienmat_thongtinchinhIUltraGrid.Rows(groupByRowsCount)
                MessageBox.Show("sum groupby : " & groupbyrow.Rows.SummaryValues("tienvnd").ToString)
            Else
                ' Write code here to process the regular by row.

                MessageBox.Show("not groupby : " & row.Cells("donvi").Value)
                rowsCount += 1
            End If

            ' If the row has any child rows, then process them too.
            If row.HasChild(False) Then
                Me.TraverseAllRowsHelper(row.GetChild(ChildRow.First), rowsCount, groupByRowsCount)
            End If

            row = row.GetSibling(SiblingRow.Next, True, False)
        End While

    End Sub
    Private Sub DmkhachhangIUltraGrid_DoubleClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickCellEventArgs) Handles DmkhachhangIUltraGrid.DoubleClickCell
        Try
            Me.KetoantienmatIUltraGrid_ThongTin.ActiveRow.Cells("donvi").Value = DmkhachhangIUltraGrid.ActiveRow.Cells("ma").Value
        Catch ex As Exception
        End Try
    End Sub

    Private Sub DmkhachhangIUltraGrid1_DoubleClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickCellEventArgs) Handles DmkhachhangIUltraGrid1.DoubleClickCell
        Try
            Me.Ketoantienmat_thongtinchinhIUltraGrid.ActiveRow.Cells("donvi").Value = DmkhachhangIUltraGrid1.ActiveRow.Cells("ma").Value
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Ketoantienmat_thongtinthueIUltraGrid_AfterCellListCloseUp(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles Ketoantienmat_thongtinthueIUltraGrid.AfterCellListCloseUp
    End Sub

    Private Sub Ketoantienmat_thongtinthueIUltraGrid_AfterCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles Ketoantienmat_thongtinthueIUltraGrid.AfterCellUpdate
        tinhDong_ThongTinThue()
    End Sub

    Private Sub Ketoantienmat_thongtinchinhIUltraGrid_TimKiem_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.ClickCell
        Try
            Me.KetoantienmatBindingSource.Position = Me.KetoantienmatBindingSource.Find("ma", Me.Ketoantienmat_thongtinchinhIUltraGrid_TimKiem.ActiveRow.Cells("ma").Value.ToString)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Ketoantienmat_thongtinthueIUltraGrid_TimKiem_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles Ketoantienmat_thongtinthueIUltraGrid_TimKiem.ClickCell
        Try
            Me.KetoantienmatBindingSource.Position = Me.KetoantienmatBindingSource.Find("ma", Me.Ketoantienmat_thongtinthueIUltraGrid_TimKiem.ActiveRow.Cells("ma").Value.ToString)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        Me.ultraProgressBar1.IncrementValue(10)
        If ultraProgressBar1.Value = ultraProgressBar1.Maximum / 2 Then
            loadDuLieu()
        End If
        If ultraProgressBar1.Value = ultraProgressBar1.Maximum Then
            setDoiTuong()
            Me.Timer1.Stop()
            Me.ultraProgressBar1.Hide()
        End If
    End Sub

End Class